package com.qf.cardlikeboot.service.impl;

import com.qf.cardlikeboot.entity.Crad;
import com.qf.cardlikeboot.mapper.CradMapper;
import com.qf.cardlikeboot.service.CradService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class CradServiceImpl implements CradService {

    @Autowired
    CradMapper cradMapper;

    @Override
    public List<Crad> selectAll() {
        return cradMapper.selectAll();
    }

    @Override
    public int insert(Crad crad) {
        return cradMapper.insertSelective(crad);
    }

    @Override
    public Crad selectById(Integer id) {
        Crad crad = cradMapper.selectByPrimaryKey(Long.valueOf(id));
        crad.setVisitied(crad.getVisitied()+1);
        cradMapper.updateByPrimaryKeySelective(crad);
        return crad;
    }

    @Override
    public List<Crad> selectLiked(String liked) {
        List<String> strings = Arrays.asList(liked.split(","));
        List<Crad> crads = cradMapper.selectLiked(strings);
        return crads;
    }

    @Override
    public Crad selectByPrimaryKey(Integer id) {
        return cradMapper.selectByPrimaryKey(Long.valueOf(id));
    }
}
